﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;

namespace Organizer.Model
{
    [Table]
    public class Organization : Base<Organization>
    {
        [Column(IsPrimaryKey = true, DbType = "UniqueIdentifier NOT NULL", CanBeNull = false, AutoSync = AutoSync.OnInsert, UpdateCheck = UpdateCheck.Never)]
        public Guid Id { get; private set; }

        private string _name;
        /// <summary>
        /// Название категории
        /// </summary>
        [Column]
        public string Name
        {
            get { return _name; }
            set 
            {
                NotifyPropertyChanging(() => this.Name);
                _name = value;
                NotifyPropertyChanged(() => this.Name);                
            }
        }

        public ICommand Delete { get; set; }

        private string _description;
        /// <summary>
        /// Описание категории
        /// </summary>
        [Column]
        public string Description
        {
            get { return _description; }
            set
            {
                NotifyPropertyChanging(() => this.Description);
                _description = value;
                NotifyPropertyChanged(() => this.Description);
            }
        }

        //private readonly EntitySet<Man> _mans;
        //[Association(Storage = "_mans", OtherKey = "_organizationId", ThisKey = "Id", DeleteRule = "CASCADE")]
        //public EntitySet<Man> Mans
        //{
        //    get { return _mans; }
        //    set { _mans.Assign(value); }
        //}

        #region construtors
        
        public Organization()
        {
            Id = Guid.NewGuid();
            this.Delete = new RelayCommand(() => { this.DeleteOrganization(); });
        }
        
        public Organization(string pName)
            : this()
        {
            Name = pName;
        }

        public Organization(string pName, string pDescription)
            : this()
        {
            Name = pName;
            Description = pDescription;
        }

        #endregion

        private void DeleteOrganization()
        {
            try
            {
                OrganizerDataContext.Instance.Organizations.DeleteOnSubmit(this);
                OrganizerDataContext.Instance.SubmitChanges();
            }
            catch (Exception)
            {
                MessageBox.Show((this).Name);
            }
        }

        public IEnumerable<Task> AssignedTasks
        {
            get
            {
                List<Task> tasks = new List<Task>();
                OrganizerDataContext.Instance.Tasks.ToList().ForEach(
                    x =>
                        {
                            if (x.LinkedMan != null && x.LinkedMan.Organization == this)
                            {
                                tasks.Add(x);
                            }
                            if (x.ResponsibleMan != null && x.ResponsibleMan.Organization == this)
                            {
                                tasks.Add(x);
                            }
                        });
                return tasks;
            }
        }

        public static Organization Create(Organization organization)
        {
            var org = new Organization();
            org.Name = organization.Name;
            org.Description = organization.Description;
            return org;
        }

        public override string ToString()
        {
            return this.Name;
        }

        public class SortByName : IComparer<Organization>
        {
            public int Compare(Organization x, Organization y)
            {
                return x.Name.CompareTo(y.Name);
            }
        }

        public static Organization None
        {
            get
            {
                return new Organization {Id = Guid.Empty, Description = string.Empty, Name = string.Empty };
            }
        }
    }
}
